package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.BillFeeWriteOffBiz;
import com.egao.orderB.entity.BillFeeWriteOff;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-04-19 17:01:51
 * @version ：1.0
 */
@Repository
public class BillFeeWriteOffBizImpl extends BaseDao implements BillFeeWriteOffBiz {

	@Override
	public JsonResult list(PageResult2<T> page, BillFeeWriteOff billFeeWriteOff, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<BillFeeWriteOff> rm = new BeanPropertyRowMapper<BillFeeWriteOff>(BillFeeWriteOff.class);
		StringBuffer sql = new StringBuffer("select b.id,b.write_off_code,b.invoicename");
        sql.append(",b.subject,b.price,b.profit_loss,b.type");
		sql.append(",b.remark,b.create_time,b.create_user,b.update_time");
		sql.append(",b.update_user");
        sql.append(",suser.username createName ");
        sql.append(" from chf_bill_fee_write_off b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" where b.deleted=0 ");
    	if(Utils.isNotEmpty(billFeeWriteOff)){
			if(Utils.isNotEmpty(billFeeWriteOff.getWriteOffCode())){
				sql.append(" and instr(b.write_off_code,:write_off_code)>0 ");
				map.put("write_off_code", billFeeWriteOff.getWriteOffCode());
			}
			if(Utils.isNotEmpty(billFeeWriteOff.getInvoicename())){
				sql.append(" and instr(b.invoicename,:invoicename)>0 ");
				map.put("invoicename", billFeeWriteOff.getInvoicename());
			}
			if(Utils.isNotEmpty(billFeeWriteOff.getDate1())){
				sql.append(" and b.create_time >= :date1 ");
				map.put("date1", billFeeWriteOff.getDate1());
			}
			if(Utils.isNotEmpty(billFeeWriteOff.getDate2())){
				sql.append(" and b.create_time<=:date2 ");
				map.put("date2", billFeeWriteOff.getDate2());
			}

			if(Utils.isNotEmpty(billFeeWriteOff.getSort())&&Utils.isNotEmpty(billFeeWriteOff.getOrder())){
                sql.append(" order by b."+billFeeWriteOff.getSort()+" "+billFeeWriteOff.getOrder());
            } else {
                sql.append(" order by b.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<BillFeeWriteOff> list(BillFeeWriteOff billFeeWriteOff, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<BillFeeWriteOff> rm = new BeanPropertyRowMapper<BillFeeWriteOff>(BillFeeWriteOff.class);
        StringBuffer sql = new StringBuffer("select b.id,b.write_off_code,b.invoicename");
        sql.append(",b.subject,b.price,b.profit_loss,b.type");
		sql.append(",b.remark,b.create_time,b.create_user,b.update_time");
		sql.append(",b.update_user");
        sql.append(",suser.username createName ");
        sql.append(" from chf_bill_fee_write_off b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" where b.deleted=0 ");
    	if(Utils.isNotEmpty(billFeeWriteOff)){
//			if(Utils.isNotEmpty(billFeeWriteOff.getName())){
//				sql.append(" and instr(b.name,:name)>0 ");
//				map.put("name", billFeeWriteOff.getName());
//			}
            if(Utils.isNotEmpty(billFeeWriteOff.getSort())&&Utils.isNotEmpty(billFeeWriteOff.getOrder())){
				sql.append(" order by b."+billFeeWriteOff.getSort()+" "+billFeeWriteOff.getOrder());
			} else {
				sql.append(" order by b.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public BillFeeWriteOff getById(BillFeeWriteOff billFeeWriteOff) {
		RowMapper<BillFeeWriteOff> rm = new BeanPropertyRowMapper<BillFeeWriteOff>(BillFeeWriteOff.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select b.id,b.write_off_code,b.invoicename");
        sql.append(",b.subject,b.price,b.profit_loss,b.type");
		sql.append(",b.remark,b.create_time,b.create_user,b.update_time");
		sql.append(",b.update_user");
        sql.append(",suser.username createName ");
        sql.append(" from chf_bill_fee_write_off b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" where b.deleted=0 and b.id=:id ");
		map.put("id", billFeeWriteOff.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public Integer save(BillFeeWriteOff billFeeWriteOff, User loginUser) {
		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
		KeyHolder keyHolder=new GeneratedKeyHolder();
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_bill_fee_write_off (");
        sql.append("write_off_code,invoicename,subject,price");
		sql.append(",profit_loss,type,remark,create_user,create_time) values ( ");
        sql.append(":write_off_code,:invoicename,:subject,:price");
		sql.append(",:profit_loss,:type,:remark,:create_user,now())");
        map.put("write_off_code", billFeeWriteOff.getWriteOffCode());
        map.put("invoicename", billFeeWriteOff.getInvoicename());
        map.put("subject", billFeeWriteOff.getSubject());
        map.put("price", billFeeWriteOff.getPrice());
        map.put("profit_loss", billFeeWriteOff.getProfitLoss());
        map.put("type", billFeeWriteOff.getType());
        map.put("remark", billFeeWriteOff.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		//		System.err.println("受到影响的行数=" + retVal + ",
		int retVal = this.jdbcTemplate.update(sql.toString(), new SqlParameterSource() {
			@Override
			public boolean hasValue(String paramName) {
				return map.containsKey(paramName);
			}
			@Override
			public Object getValue(String paramName) throws IllegalArgumentException {
				return map.get(paramName);
			}
		}, generatedKeyHolder);
		Integer id = generatedKeyHolder.getKey().intValue();
		return id;
	}

	@Override
	public boolean update(BillFeeWriteOff billFeeWriteOff, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_bill_fee_write_off set ");
        sql.append("write_off_code=:write_off_code,invoicename=:invoicename,subject=:subject ");
		sql.append(",price=:price,profit_loss=:profit_loss,type=:type ");
		sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
        map.put("write_off_code", billFeeWriteOff.getWriteOffCode());
        map.put("invoicename", billFeeWriteOff.getInvoicename());
        map.put("subject", billFeeWriteOff.getSubject());
        map.put("price", billFeeWriteOff.getPrice());
        map.put("profit_loss", billFeeWriteOff.getProfitLoss());
        map.put("type", billFeeWriteOff.getType());
        map.put("remark", billFeeWriteOff.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", billFeeWriteOff.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(BillFeeWriteOff billFeeWriteOff, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(billFeeWriteOff.getId().length()>0){
			for(String ID : billFeeWriteOff.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_bill_fee_write_off set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
